package com.iknow;

import com.iknow.http.HTMLEntity;
import com.iknow.http.HttpException;
import com.iknow.http.Response;
import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.TimeZone;
import org.json.JSONException;
import org.json.JSONObject;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class IKnowResponse
  implements Serializable
{
  private static Map<String, SimpleDateFormat> formatMap = new HashMap();
  private static final long serialVersionUID = -1606669415L;
  private transient int rateLimitLimit = -1;
  private transient int rateLimitRemaining = -1;
  private transient long rateLimitReset = -1L;

  public IKnowResponse(Response paramResponse)
  {
    String str1 = paramResponse.getResponseHeader("X-RateLimit-Limit");
    if (str1 != null)
      this.rateLimitLimit = Integer.parseInt(str1);
    String str2 = paramResponse.getResponseHeader("X-RateLimit-Remaining");
    if (str2 != null)
      this.rateLimitRemaining = Integer.parseInt(str2);
    String str3 = paramResponse.getResponseHeader("X-RateLimit-Reset");
    if (str3 != null)
      this.rateLimitReset = Long.parseLong(str3);
  }

  protected static void ensureRootNodeNameIs(String paramString, Document paramDocument)
    throws HttpException
  {
    Element localElement = paramDocument.getDocumentElement();
    if (!(paramString.equals(localElement.getNodeName())))
      throw new HttpException("Unexpected root node name:" + localElement.getNodeName() + ". Expected:" + paramString + ". Check the availability of the Weibo API at http://open.t.sina.com.cn/");
  }

  protected static void ensureRootNodeNameIs(String paramString, Element paramElement)
    throws HttpException
  {
    if (!(paramString.equals(paramElement.getNodeName())))
      throw new HttpException("Unexpected root node name:" + paramElement.getNodeName() + ". Expected:" + paramString + ". Check the availability of the Weibo API at http://open.t.sina.com.cn/.");
  }

  protected static void ensureRootNodeNameIs(String[] paramArrayOfString, Element paramElement)
    throws HttpException
  {
    String str2;
    int k;
    String str1 = paramElement.getNodeName();
    int i = paramArrayOfString.length;
    int j = 0;
    if (j >= i)
    {
      str2 = "";
      k = 0;
    }
    while (true)
    {
      while (k >= paramArrayOfString.length)
      {
        throw new HttpException("Unexpected root node name:" + paramElement.getNodeName() + ". Expected:" + str2 + ". Check the availability of the Weibo API at http://open.t.sina.com.cn/.");
        if (paramArrayOfString[j].equals(str1))
          return;
        ++j;
      }
      if (k != 0)
        str2 = str2 + " or ";
      str2 = str2 + paramArrayOfString[k];
      ++k;
    }
  }

  protected static boolean getBoolean(String paramString, JSONObject paramJSONObject)
    throws JSONException
  {
    boolean bool;
    String str = paramJSONObject.getString(paramString);
    if ((str == null) || ("".equals(str)) || ("null".equals(str)))
      bool = false;
    while (true)
    {
      return bool;
      bool = Boolean.valueOf(str).booleanValue();
    }
  }

  protected static boolean getChildBoolean(String paramString, Element paramElement)
  {
    return Boolean.valueOf(getTextContent(paramString, paramElement)).booleanValue();
  }

  protected static Date getChildDate(String paramString, Element paramElement)
    throws HttpException
  {
    return getChildDate(paramString, paramElement, "EEE MMM d HH:mm:ss z yyyy");
  }

  protected static Date getChildDate(String paramString1, Element paramElement, String paramString2)
    throws HttpException
  {
    return parseDate(getChildText(paramString1, paramElement), paramString2);
  }

  protected static int getChildInt(String paramString, Element paramElement)
  {
    int i;
    String str = getTextContent(paramString, paramElement);
    if ((str == null) || ("".equals(str)) || ("null".equals(paramString)))
      i = -1;
    while (true)
    {
      return i;
      i = Integer.valueOf(str).intValue();
    }
  }

  protected static long getChildLong(String paramString, Element paramElement)
  {
    long l;
    String str = getTextContent(paramString, paramElement);
    if ((str == null) || ("".equals(str)) || ("null".equals(paramString)))
      l = -1L;
    while (true)
    {
      return l;
      l = Long.valueOf(str).longValue();
    }
  }

  protected static String getChildString(String paramString, Element paramElement)
  {
    String str2;
    String str1 = getTextContent(paramString, paramElement);
    if ((str1 == null) || ("".equals(str1)) || ("null".equals(paramString)))
      str2 = "";
    while (true)
    {
      return str2;
      str2 = String.valueOf(str1);
    }
  }

  protected static String getChildText(String paramString, Element paramElement)
  {
    return HTMLEntity.unescape(getTextContent(paramString, paramElement));
  }

  protected static int getInt(String paramString, JSONObject paramJSONObject)
    throws JSONException
  {
    int i;
    String str = paramJSONObject.getString(paramString);
    if ((str == null) || ("".equals(str)) || ("null".equals(str)))
      i = -1;
    while (true)
    {
      return i;
      i = Integer.parseInt(str);
    }
  }

  protected static long getLong(String paramString, JSONObject paramJSONObject)
    throws JSONException
  {
    long l;
    String str = paramJSONObject.getString(paramString);
    if ((str == null) || ("".equals(str)) || ("null".equals(str)))
      l = -1L;
    while (true)
    {
      return l;
      l = Long.parseLong(str);
    }
  }

  protected static String getString(String paramString, JSONObject paramJSONObject)
    throws JSONException
  {
    String str2;
    String str1 = paramJSONObject.getString(paramString);
    if ((str1 == null) || ("".equals(str1)) || ("null".equals(str1)))
      str2 = "";
    while (true)
    {
      return str2;
      str2 = String.valueOf(str1);
    }
  }

  protected static String getString(String paramString, JSONObject paramJSONObject, boolean paramBoolean)
  {
    Object localObject = null;
    try
    {
      String str1 = paramJSONObject.getString(paramString);
      localObject = str1;
      if (paramBoolean);
    }
    catch (JSONException localJSONException)
    {
      try
      {
        String str2 = URLDecoder.decode(localObject, "UTF-8");
        localObject = str2;
        return localObject;
        localJSONException = localJSONException;
      }
      catch (UnsupportedEncodingException localUnsupportedEncodingException)
      {
      }
    }
  }

  protected static String getTextContent(String paramString, Element paramElement)
  {
    String str1;
    NodeList localNodeList = paramElement.getElementsByTagName(paramString);
    if (localNodeList.getLength() > 0)
    {
      Node localNode = localNodeList.item(0).getFirstChild();
      if (localNode != null)
      {
        String str2 = localNode.getNodeValue();
        if (str2 != null)
          str1 = str2;
      }
    }
    while (true)
    {
      while (true)
      {
        return str1;
        str1 = "";
      }
      str1 = "";
    }
  }

  protected static boolean isRootNodeNilClasses(Document paramDocument)
  {
    int i;
    String str = paramDocument.getDocumentElement().getNodeName();
    if ((!("nil-classes".equals(str))) && (!("nilclasses".equals(str))))
      i = 0;
    while (true)
    {
      return i;
      i = 1;
    }
  }

  public static Date parseDate(String paramString1, String paramString2)
    throws HttpException
  {
    Date localDate;
    if ((paramString1 == null) || ("".equals(paramString1)))
    {
      localDate = null;
      return localDate;
    }
    SimpleDateFormat localSimpleDateFormat = (SimpleDateFormat)formatMap.get(paramString2);
    if (localSimpleDateFormat == null)
    {
      localSimpleDateFormat = new SimpleDateFormat(paramString2, Locale.CHINA);
      localSimpleDateFormat.setTimeZone(TimeZone.getTimeZone("GMT+8"));
      formatMap.put(paramString2, localSimpleDateFormat);
    }
    try
    {
      monitorenter;
    }
    catch (ParseException localParseException)
    {
      try
      {
        while (true)
        {
          localDate = localSimpleDateFormat.parse(paramString1);
          monitorexit;
        }
      }
      finally
      {
        localObject = finally;
        monitorexit;
        throw localObject;
        localParseException = localParseException;
        throw new HttpException("Unexpected format(" + paramString1 + ") returned from sina.com.cn");
      }
    }
  }

  public int getRateLimitLimit()
  {
    return this.rateLimitLimit;
  }

  public int getRateLimitRemaining()
  {
    return this.rateLimitRemaining;
  }

  public long getRateLimitReset()
  {
    return this.rateLimitReset;
  }
}